package org.ko.commons.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @文件名称 :DateUtil.java
 * @所在包 :com.nerve.human.common.util
 * @功能描述 :
 *	时间格式工具类
 * @创建者 :集成显卡	1053214511@qq.com
 * @公司：IBM GDC
 * @创建日期 :2013-4-9
 * @log :
 */
public class DateUtil {
	
	/**
	 * @method name: toDate
	 * @description: 将指定的时间字符串转换为Date对象，默认的时间格式：yyyy-MM-dd hh:mm
	 * @return type: Date
	 *	@param timeString
	 *	@return
	 *	@throws Exception
	 */
	public static Date toDate(String timeString) throws Exception{
		return new SimpleDateFormat("yyyy-MM-dd hh:mm").parse(timeString);
	}
	
	
	public static Date toDate(String timeString, String format) throws Exception{
		return new SimpleDateFormat(format).parse(timeString);
	}
	
	/**
	 * 获取指定当前时间
	 *	@param timeType 时间串格式
	 *	@return
	 *  @date: 2013-4-9
	 */
	public static String getDate(String timeType) {
		String strTime = null;
		try {
			SimpleDateFormat simpledateformat = new SimpleDateFormat(timeType);
			strTime = simpledateformat.format(new Date());
		} catch (Exception ex) {
			System.err.println("格式化日期错误 : " + ex.getMessage());
		}
		return strTime;
	}
	
	/**
	 * 
	 * @method name: toString
	 * @return type: String
	 *	@param date
	 *	@param format
	 *	@return
	 */
	public static String toString(Date date, String format){
		String strTime = null;
		try {
			SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
			strTime = simpledateformat.format(date);
		} catch (Exception ex) {
			System.err.println("格式化日期错误 : " + ex.getMessage());
		}
		return strTime;
	}

	/**
	 * 获取当前时间，格式为：yyyy-MM-dd HH:mm:ss
	 *	@return
	 *  @date: 2013-4-9
	 */
	public static String getDate() {
		return getDate("yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * 判断是否为当天时间
	 *	@param comparedDate	格式为：yyyy-MM-dd
	 *	@return
	 *  @date: 2013-4-9
	 */
	public static boolean isToday(String comparedDate) {
		return getDate("yyyy-MM-dd").equals(comparedDate);
	}
	
	public static String getBeforeDate(String timePos, int day) {
		long dateLong = 0L;
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");

		if ((timePos == null) || ("".equals(timePos)))
			dateLong = new Date().getTime();
		else {
			try {
				dateLong = dateFormat.parse(timePos).getTime();
			} catch (ParseException e) {
				System.err.println("输入时间\"" + timePos + "\"不合法,parse时间错误 : "
						+ e.getMessage());
				return "0000:00:00 00:00:00";
			}
		}
		dateLong -= day * 24 * 60 * 60 * 1000;
		return dateFormat.format(new Date(dateLong));
	}

	public static String getBeforeDate(String timeType, String timePos, int day) {
		long dateLong = 0L;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeType);

		if ((timePos == null) || ("".equals(timePos)))
			dateLong = new Date().getTime();
		else {
			try {
				dateLong = simpleDateFormat.parse(timePos).getTime();
			} catch (ParseException e) {
				System.err.println("请检查\"" + timePos + "\"和\"" + timeType
						+ "\"的时间格式是否一样,parse时间错误 : " + e.getMessage());
				return "0000:00:00 00:00:00";
			}
		}
		dateLong -= day * 24 * 60 * 60 * 1000;
		return simpleDateFormat.format(new Date(dateLong));
	}

	public static String getAfterDate(String timeType, String timePos, int day) {
		long dateLong = 0L;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeType);

		if ((timePos == null) || ("".equals(timePos)))
			dateLong = new Date().getTime();
		else {
			try {
				dateLong = simpleDateFormat.parse(timePos).getTime();
			} catch (ParseException e) {
				System.err.println("请检查\"" + timePos + "\"和\"" + timeType
						+ "\"的时间格式是否一样,parse时间错误 : " + e.getMessage());
				return "0000:00:00 00:00:00";
			}
		}
		dateLong += day * 24 * 60 * 60 * 1000;
		return simpleDateFormat.format(new Date(dateLong));
	}

	/**
	 * 获取date 值所在的当天及下一天的日期，这个日期的时，分都是置0的
	 * @param date
	 * @return
	 */
	public static Date[] getDateWithinOneDay(Date date){
		Date result[]=new Date[2];
		Calendar c=Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 1);
		result[0]=c.getTime();
		c.add(Calendar.DAY_OF_MONTH, 1);
		result[1]=c.getTime();
		return result;
	}
	
	/**
	 * 计算两个时间的分钟差
	 *	@param startDate
	 *	@param endDate
	 *	@return
	 *  @date: 2013-4-9
	 */
	public static int getMinute(Date startDate, Date endDate) {
		return (int) ((endDate.getTime() - startDate.getTime()) / 1000L / 60L);
	}
	
	/**
	 * 计算两个时间的天数差
	 * @method name: getDay
	 * @return type: int
	 *	@param stDate
	 *	@param endDate
	 *	@return
	 */
	public static int getDay(Date stDate, Date endDate){
		return (int) ((endDate.getTime() - stDate.getTime()) / 1000L / 60L / 60L / 24L);
	}
	
	/**
	 * 获取当月的第一个星期一（以中国为例）
	 * @method name: getFirstMonday
	 * @return type: void
	 */
	public static Date getFirstMondayOfMonth(String month) throws Exception{
		return getFirstMondayOfMonth(month, "yyyy-MM");
	}
	
	/**
	 * 获取当月的第一个星期一（以中国为例）
	 * @method name: getFirstMonday
	 * @return type: void
	 */
	public static Date getFirstMondayOfMonth(String dateString, String dateFormat) throws Exception{
		Date date = DateUtil.toDate(dateString, dateFormat);
		
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		
		int step = (9 - c.get(Calendar.DAY_OF_WEEK)) % 7;
		c.add(Calendar.DAY_OF_YEAR, step);
		
		return c.getTime();
	}
	
	/**
	 * 
	 * @method name: figureWeekIndexOfMonth 
	 * @return type: int
	 *
	 * @param date
	 * @return
	 */
	public static int figureWeekIndexOfMonth(String dateString, String dateFormat) throws Exception{
		Calendar c = Calendar.getInstance();
		
		Date curDate = toDate(dateString, dateFormat);
		c.setTime(curDate);
		int day = c.get(Calendar.DAY_OF_MONTH);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date firstMondy = getFirstMondayOfMonth(sdf.format(c.getTime()));
		c.setTime(firstMondy);
		
		System.out.println(firstMondy);
		
		System.out.println(day+ " ------");
		int index = 0;
		do{
			c.add(Calendar.DAY_OF_MONTH, 7);
			index ++;
		}
		while(c.get(Calendar.DAY_OF_MONTH) < day);
		
		return index;
	}
	
	/**
	 * 获取今天所在周的星期一
	 * @method name: getMondyOfToday 
	 * @return type: String
	 *
	 * @return
	 */
	public static String getMondyOfToday(String format){
		Calendar c = Calendar.getInstance();
		int step = c.get(Calendar.DAY_OF_WEEK);
		//星期天
		if(step == 1)
			step = 6;
		else
			step -= 2;
		
		c.add(Calendar.DAY_OF_YEAR, -step);
		
		return toString(c.getTime(), format);
	}
	
	/**
	 * 获取今天所在周的星期天
	 * @method name: getMondyOfToday 
	 * @return type: String
	 *
	 * @return
	 */
	public static String getSundayOfToday(String format){
		Calendar c = Calendar.getInstance();
		
		int step = (9 - c.get(Calendar.DAY_OF_WEEK)) % 7;
		c.add(Calendar.DAY_OF_YEAR, -step + 6);
		
		return toString(c.getTime(), format);
	}
	
	/**
	 * 获取今天所在周的星期天
	 * @method name: getMondyOfToday 
	 * @return type: String
	 *
	 * @return
	 */
	public static Date getSundayOfToday(){
		Calendar c = Calendar.getInstance();
		
		int step = (9 - c.get(Calendar.DAY_OF_WEEK)) % 7;
		c.add(Calendar.DAY_OF_YEAR, -step + 6);
		
		return c.getTime();
	}
	
	public static void main(String[] args) throws Exception {
		//String dates = "20131025";
		
		//System.out.println(figureWeekIndexOfMonth(dates, "yyyyMMdd"));
		
		//System.out.println(ZhousanOfToday("yyyy-MM-dd"));
	}
}